<%--
  Created by IntelliJ IDEA.
  User: abb
  Date: 2018-07-11
  Time: 11:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="/static/js/jquery-easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/static/js/jquery-easyui/themes/icon.css">
    <script type="text/javascript" src="/static/js/jquery-easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery-easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
    <script>

        $(function () {
            var dictionaryItem_datagrid = $("#dictionaryItem_datagrid");
            var dictionary_datagrid = $("#dictionary_datagrid");
            var dictionary_dialog = $("#dictionary_dialog");
            var dictionary_form = $("#dictionary_form");

            var dictionaryItem_datagrid = $("#dictionaryItem_datagrid");
            var dictionaryItem_dialog = $("#dictionaryItem_dialog");
            var dictionaryItem_form = $("#dictionaryItem_form");

            var methodObj = {
                add: function () {
                    dictionary_form.form("clear");
                    dictionary_dialog.dialog("setTitle", "新增目录");
                    dictionary_dialog.dialog("open");
                },
                edit: function () {
                    var systemDictionary = dictionary_datagrid.datagrid("getSelected");

                    if (!systemDictionary) {
                        $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                        return;
                    }

                    dictionary_form.form("clear");
                    dictionary_dialog.dialog("setTitle", "字典目录编辑");

                    dictionary_form.form("load", systemDictionary);

                    dictionary_dialog.dialog("open");
                },
                remove: function () {
                    var systemDictionary = dictionary_datagrid.datagrid("getSelected");

                    if (!systemDictionary) {
                        $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                        return;
                    }

                    $.messager.confirm("确认窗口", "确认要删除该字典目录?", function (r) {
                        if (r) {
                            $.post("/systemDictionary/delete", {id: systemDictionary.id}, function (data) {
                                if (data.success) {
                                    $.messager.alert("温馨提示", "成功", "info", function () {
                                        dictionary_datagrid.datagrid("reload");
                                    })
                                } else {
                                    $.messager.alert("温馨提示", data.msg, "info");
                                }
                            });
                        }
                    })
                },
                reload: function () {
                    dictionary_datagrid.datagrid("reload");
                },
                save:function () {

                    var url = "/systemDictionary/save";
                    if($("#deptId").val()){
                        url = "/systemDictionary/update";
                    }

                    $("#dictionary_form").form('submit', {
                        url: url,

                        success: function(data){
                            data = $.parseJSON(data);
                            if(data.success){
                                $.messager.alert("提示","成功","info",function () {
                                    methodObj.cancel();
                                    methodObj.reload();
                                })
                            }else{
                                $.messager.alert("提示",data.msg,"error");
                            }
                        }
                    });

                },
                cancel: function () {
                    dictionary_dialog.dialog("close");
                }
            }

            dictionary_datagrid.datagrid({
                url: '/systemDictionary/list',
                fit: true,
                fitColumns: true,
                rownumbers: true,
                striped: true,
                singleSelect: true,
                toolbar: '#dictionary_toolbar',
                onSelect: function (index, row) {
                    var id = row.id;
                    $("#dictionaryItem_datagrid").datagrid({url: "/systemDictionaryItem/listItem?id=" + id})
                },
                columns: [[
                    {field: 'sn', title: '字典目录编码', width: 100, align: 'center'},
                    {field: 'name', title: '字典目录名称', width: 100, align: 'center'},
                    {field: 'intro', title: '字典目录简介', width: 100, align: 'center'}
                ]]

            });

            dictionary_dialog.dialog({
                width: 300,
                height: 250,
                closed: true,
                buttons: '#dictionary_btns'
            })
            dictionaryItem_datagrid.datagrid({

                fit: true,
                fitColumns: true,
                rownumbers: true,
                striped: true,
                singleSelect: true,
                toolbar: '#dictionaryItem_toolbar',
                columns: [[
                    {field: 'name', title: '字典明细名称', width: 100, align: 'center'},
                    {
                        field: 'parent', title: '字典目录', width: 100, align: 'center', formatter: function (value, row, index) {
                        return value.name;
                    }
                    },
                    {field: 'intro', title: '字典明细简介', width: 100, align: 'center'}
                ]]
            })

            dictionaryItem_dialog.dialog({
                width: 300,
                height: 250,
                closed: true,
                buttons: '#dictionaryItem_btns'
            })





            var itemmethodObj = {
                itemAdd: function () {
                    dictionaryItem_form.form("clear");
                    dictionaryItem_dialog.dialog("setTitle", "新增目录");
                    dictionaryItem_dialog.dialog("open");
                },
                itemEdit: function () {
                    var systemDictionaryItem = dictionaryItem_datagrid.datagrid("getSelected");

                    if (!systemDictionaryItem) {
                        $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                        return;
                    }

                    dictionaryItem_form.form("clear");
                    dictionaryItem_dialog.dialog("setTitle", "字典目录编辑");

                    systemDictionaryItem["parent.id"]=systemDictionaryItem.parent.id;

                    dictionaryItem_form.form("load", systemDictionaryItem);

                    dictionaryItem_dialog.dialog("open");
                },
                itemRemove: function () {
                    var systemDictionaryItem = dictionaryItem_datagrid.datagrid("getSelected");

                    if (!systemDictionaryItem) {
                        $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                        return;
                    }

                    $.messager.confirm("确认窗口", "确认要删除该条明细?", function (r) {
                        if (r) {
                            $.get("/systemDictionaryItem/delete", {id: systemDictionaryItem.id}, function (data) {
                                if (data.success) {
                                    $.messager.alert("温馨提示", "成功", "info", function () {
                                        dictionaryItem_datagrid.datagrid("reload");
                                    })
                                } else {
                                    $.messager.alert("温馨提示", data.msg, "info");
                                }
                            });
                        }
                    })
                },
                itemReload: function () {
                    dictionaryItem_datagrid.datagrid("reload");
                },
                itemSave:  function () {

                    var url = "/systemDictionaryItem/save";
                    if($("#itemId").val()){
                        url = "/systemDictionaryItem/update";
                    }

                    $("#dictionaryItem_form").form('submit', {
                        url: url,

                        success: function(data){
                            data = $.parseJSON(data);
                            if(data.success){
                                $.messager.alert("提示","成功","info",function () {
                                    itemmethodObj.itemCancel();
                                    itemmethodObj.itemReload();
                                })
                            }else{
                                $.messager.alert("提示",data.msg,"error");
                            }
                        }
                    });

                },
                itemCancel: function () {
                    dictionaryItem_dialog.dialog("close");
                }
            }


            // 给按钮绑定事件
            $("a[data-item_cmd]").click(function () {
                var item_cmd = $(this).data("item_cmd");
                itemmethodObj[item_cmd]();

            });
            // 给按钮绑定事件
            $("a[data-cmd]").click(function () {
                var cmd = $(this).data("cmd");
                methodObj[cmd]();

            });

        })
    </script>
</head>

<body>
<div id="cc" class="easyui-layout" data-options="fit:true">
    <div data-options="region:'west',title:'字典目录',split:true" style="width:600px;">
        <table id="dictionary_datagrid" class="easyui-datagrid"></table>
    </div>

    <div data-options="region:'center',title:'字典目录明细'" style="padding:5px;background:#eee;">
        <table id="dictionaryItem_datagrid" class="easyui-datagrid"></table>
    </div>
</div>

<div id="dictionary_dialog">
    <form id="dictionary_form" method="post" style="margin-left: 35px;margin-top: 20px">
        <input type="hidden" name="id" id="id">
        <input type="text" class="easyui-textbox"
               data-options="label:'字典目录编码:', labelPosition: 'left',width:200" name="sn">
        <br>
        <br>
        <input type="text" class="easyui-textbox"
               data-options="label:'字典目录名称:', labelPosition: 'left',width:200" name="name">
        <br>
        <br>
        <input type="text" class="easyui-textbox"
               data-options="label:'字典目录介绍:', labelPosition: 'left',width:200" name="intro">
    </form>
</div>

<div id="dictionaryItem_dialog">
    <form id="dictionaryItem_form" method="post" style="margin-left: 50px;margin-top: 20px">
        <input type="hidden" name="id" id="itemId">
        <input type="text" class="easyui-textbox"
               data-options="label:'条目名称:', labelPosition: 'left',width:200" name="name">
        <br>
        <br>
        <input type="text" class="easyui-textbox"
               data-options="label:'条目简介:', labelPosition: 'left',width:200" name="intro">
        <br>
        <br>
        <span>字典目录：</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="text" class="easyui-combobox"
               data-options="valueField:'id',width:115,textField:'name',url:'/systemDictionary/list'" name="parent.id">
    </form>
</div>


<div id="dictionary_toolbar">
    <a class="easyui-linkbutton" iconCls="icon-add" plain="true" data-cmd="add">新增</a>
    <a class="easyui-linkbutton" iconCls="icon-edit" plain="true" data-cmd="edit">编辑</a>
    <a class="easyui-linkbutton" iconCls="icon-remove" plain="true" data-cmd="remove">删除</a>
    <a class="easyui-linkbutton" iconCls="icon-reload" plain="true" data-cmd="reload">刷新</a>
</div>

<div id="dictionary_btns">
    <a class="easyui-linkbutton" iconCls="icon-save" plain="true" data-cmd="save">保存</a>
    <a class="easyui-linkbutton" iconCls="icon-cancel" plain="true" data-cmd="cancel">取消</a>
</div>


<div id="dictionaryItem_toolbar">
    <a class="easyui-linkbutton" iconCls="icon-add" plain="true" data-item_cmd="itemAdd">新增</a>
    <a class="easyui-linkbutton" iconCls="icon-edit" plain="true" data-item_cmd="itemEdit">编辑</a>
    <a class="easyui-linkbutton" iconCls="icon-remove" plain="true" data-item_cmd="itemRemove">删除</a>
    <a class="easyui-linkbutton" iconCls="icon-reload" plain="true" data-item_cmd="itemReload">刷新</a>
</div>

<div id="dictionaryItem_btns">
    <a class="easyui-linkbutton" iconCls="icon-save" plain="true" data-item_cmd="itemSave">保存</a>
    <a class="easyui-linkbutton" iconCls="icon-cancel" plain="true" data-item_cmd="itemCancel">取消</a>
</div>
</body>
</html>
